package com.vito.oa.act.action;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.dubbo.config.annotation.Reference;
import com.vito.act.approval.model.ActApprovalProgressModel;
import com.vito.base.authoriza.action.BaseAction;
import com.vito.base.authoriza.cache.SessionMap;
import com.vito.base.authoriza.model.UserModel;
import com.vito.base.model.ResMsg;
import com.vito.base.model.Result;
import com.vito.oa.act.extend.model.OaActExBussModel;
import com.vito.oa.act.service.OaActProcessService;

@Controller
@RequestMapping("oa/act/process")
public class OaActProcessAction extends BaseAction {
	private static final Logger log = LoggerFactory.getLogger(OaActProcessAction.class);
	@Reference
	private OaActProcessService oaActProcessService;
	
	/**
	 * 导入工作流文件
	 * @param filePath
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/importProcess")
	public Result importProcess(String filePath) {
		Result r = new Result();
		try {
			oaActProcessService.importProcess(filePath);
			r.setCode(Result.CODE_OK);
			r.setMsg("导入工作流成功");
		} catch (Exception e) {
			log.error("查询异常：",e);
			r.setCode(Result.CODE_ERROR);
			r.setMsg(ResMsg.EXECERR);
			return r;
		}
		return r;
	}
	
	/**
	 * 查询系统预制流程列表
	 * @param model
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/querySysProcess")
	public Result querySysProcessByPage(OaActExBussModel model){
		Result rs = new Result();
		try {
			UserModel user = SessionMap.getObj();
			if(null==user){
				rs.setCode(Result.CODE_LOGIN_REQUIRE);
				rs.setMsg(ResMsg.MSG_LOGIN_NEED);
				return rs;
			}
			model.setCreateDept(user.getDeptParentId());
			rs.setData(oaActProcessService.querySysProgressByPage(model));
			rs.setCode(Result.CODE_OK);
		} catch (Exception e) {
			log.error("查询异常：",e);
			rs.setCode(Result.CODE_ERROR);
			rs.setMsg(ResMsg.EXECERR);
			return rs;
		}
		return rs;	
	}

	/**
	 * 系统流程复制（暂不使用）
	 * @param process
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/copySysProcess")
	public Result importSysProcess(ActApprovalProgressModel process){
		Result rs = new Result();
		UserModel user = SessionMap.getObj();
		if(null==user){
			rs.setCode(Result.CODE_LOGIN_REQUIRE);
			rs.setMsg(ResMsg.MSG_LOGIN_NEED);
			return rs;
		}
		if(oaActProcessService.hasImportLog(process.getBusinessId(),user.getDeptParentId())){
			rs.setCode(Result.CODE_PARA_ERROR);
			rs.setMsg("该模板已被导入，请到流程管理菜单查看");
			return rs;
		}
		rs = oaActProcessService.importSysProcessToDefined(process, user);
		return rs;
	}
	
	/**
	 * 批量导入
	 * @param dir
	 * @return
	 */
	@RequestMapping("importBpmn")
	@ResponseBody
	public Result importBpmn(String dir) {
		Result rs = new Result();
		rs.setCode(Result.CODE_OK);
		rs.setMsg("导入成功");
		try{
			oaActProcessService.importBpmn(dir);
		}catch(Exception e) {
			rs.setMsg("请求失败！");
			rs.setCode(Result.CODE_ERROR);
		}
		return rs;
		
	}
	/**
	 * 批量导出
	 * @param dir
	 * @return
	 */
	@RequestMapping("exportBpmn")
	@ResponseBody
	public Result exportBpmn(String dir) {
		Result rs = new Result();
		rs.setCode(Result.CODE_OK);
		rs.setMsg("导出成功");
		try{
			oaActProcessService.exportBpmn(dir);
		}catch(Exception e) {
			rs.setMsg("请求失败！");
			rs.setCode(Result.CODE_ERROR);
		}
		return rs;
	}
	
	/**
	 * 流程卸载
	 * @param key
	 * @param DeployId
	 * @return
	 */
	@RequestMapping("uninstallBpmn")
	@ResponseBody
	public Result uninstallBpmn(String key,String DeployId) {
		Result rs = new Result();
		try{
			oaActProcessService.uninstallBpmn(key,DeployId);
			rs.setCode(Result.CODE_OK);
		}catch(Exception e) {
			rs.setMsg("请求失败！");
			rs.setCode(Result.CODE_ERROR);
		}
		return rs;
	}
}
